package org.user.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.user.dao.UserDao;
import org.user.entity.User;
import org.user.service.LoginService;
 
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //获取从页面中提交过来的数据
        String userName = request.getParameter("name");
        String userPass = request.getParameter("pass");
        
        LoginService service = new LoginService();
        //调用service方法 把用户名 密码传入给service
        User user = new User();
        try {
			BeanUtils.populate(user, request.getParameterMap());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        boolean flag = service.checkUser(user);
        String msg = flag?"success":"error";
        
        request.setAttribute("msg", msg);
        if(msg=="success"){
            request.getRequestDispatcher("index.jsp").forward(request, response);    
        }
        else{
            System.out.println("账户名或密码错误");
        }
    }

}